package org.zall_dev;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.dev_common.file.FileUtils;
import org.dev_common.json.Jacksons;

import com.google.common.collect.ImmutableMap;
import com.halo.dc.service.DCRequest;
import com.halo.kernel.utils.JsonUtils;

/**
 * logstash 匹配验证
 */
public class Y {
    public static void main(String[] args) throws Exception {
        String ss = "{\\\\\\\"sentence\\\\\\\":\\\\\\\"微信聊吧加我的号841297684467\\\\\\\\n测试\\\\\\\"}";
        String replace = ss.replaceAll("\\\\\\\\n", "");
        String replace_reg = ss.replaceAll("[\\\\]+n", "");
        System.out.println("src\t"+ss);
        System.out.println("repl\t"+replace);
        System.out.println("reg\t"+replace_reg);
        System.out.println("\\\\");
        System.out.println("\\\\\\\\n");
        List<String> list = FileUtils.listString("/Users/thomas/logstash_files/dcall.csv");
        String s2 = list.get(0);
        //System.out.println(s2.replaceAll("\\\\\\\\n", ""));
        System.out.println(s2.replaceAll("[\\\\]+n", ""));
        
//        // print_Map(list);
//        print_DC_request(list);
//        check_Json();
//        check_Json_formate();
        diff_check();
    }
    
    private static void diff_check() {
        List<String> list = FileUtils.listString("/Users/thomas/logstash_files/backslash.1");
        List<String> regexed = FileUtils.listString("/Users/thomas/logstash_files/plain");
        
        Map<String, Map> mregex = new HashMap<>();
        Map<String, Map> morigin = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            //System.out.println("========"+i+"==========");
            if (list.get(i).length() < 1) {
                continue;
            }
            Map origin = JsonUtils.fromJson(list.get(i), Map.class);
            Map regex = JsonUtils.fromJson(regexed.get(i), Map.class);
            
            Map regex_msg = JsonUtils.fromJson((String)(regex.get("msg")), Map.class);
            mregex.put(regex.get("ts").toString(), regex_msg);
            
            Map origin_msg = JsonUtils.fromJson((String)(origin.get("msg")), Map.class);
            morigin.put(origin.get("ts").toString(), origin_msg);
        }
        for (Entry<String, Map> entry : morigin.entrySet()) {
            if (entry.getValue().size() != mregex.get(entry.getKey()).size()) {
                System.out.println(entry.getKey());
            }
            Map<String,?> orig = entry.getValue();
            Map<String,?> regex = mregex.get(entry.getKey());
            for (Entry<String, ?> entry2 : orig.entrySet()) {
                if (!entry2.getValue().equals(regex.get(entry2.getKey()))) {
                    System.out.println(String.format("ts:%s, field:%s", entry.getKey(), entry2.getKey()));
                    System.out.println(entry2.getValue());
                    System.out.println(regex.get(entry2.getKey()));
                }
            }
        }
    }

    public static void check_Json_formate() {
        Request request = new Request();
        request.setSourcetype("osVersion");
        Inner inner = new Inner();
        inner.setInfo("明天就元旦了\n[抱拳]提前祝你！\n🌟✨💘2018，新的一年🎵\n");
        inner.setVersion("innner_version");
        request.setInner(inner);
        Outer outer = new Outer();
        outer.setBiz("biz");
        outer.setRequest(request);
        System.out.println(JsonUtils.toJson(outer));
        System.out.println(JsonUtils.toJson(request));
        System.out.println(JsonUtils.toJson(inner));
    }
    public static void check_Json() {
        List<String> list = FileUtils.listString("/Users/thomas/logstash_files/dcall_2.csv");
        List<String> regexed = FileUtils.listString("/Users/thomas/logstash_files/dcall3");
        for (int i = 0; i < list.size(); i++) {
            System.out.println("========"+i+"==========");
            if (list.get(i).length() < 1) {
                continue;
            }
            DCRequest origin = JsonUtils.fromJson(list.get(i), DCRequest.class);
            DCRequest regex = JsonUtils.fromJson(regexed.get(i), DCRequest.class);
            System.out.println(JsonUtils.toJson(JsonUtils.fromJson(origin.getMsg(), Map.class)));
            System.out.println(JsonUtils.toJson(JsonUtils.fromJson(regex.getMsg(), Map.class)));
        }
    }

    private static void print_DC_request(List<String> list) {
        DCRequest origin = JsonUtils.fromJson(list.get(0), DCRequest.class);
        
        DCRequest dcRequest = new DCRequest();
        dcRequest.setAppId("ZM_ZX01");
        dcRequest.setDcType("110010");
        dcRequest.setTs("1515479942091");
        dcRequest.setCreateDt("20180109143902\nline");
        String msg = origin.getMsg();
        dcRequest.setMsg(msg);
        System.out.println("source:  " + list.get(0));
        System.out.println("add\\n:   " + JsonUtils.toJson(dcRequest));
        DCRequest parsed = JsonUtils.fromJson(list.get(0).replaceAll("[\\\\]+n", ""), DCRequest.class);
        System.out.println("regexed: " + JsonUtils.toJson(parsed));
        Map map = JsonUtils.fromJson(parsed.getMsg(), Map.class);
        //System.out.println(JsonUtils.fromJson(list.get(0).replaceAll("\\\\\\\\n", ""), Map.class));
        System.out.println(JsonUtils.fromJson(list.get(0).replaceAll("[\\\\]+n", ""), Map.class));
        System.out.println(map.get("refData"));
        // 
        map = JsonUtils.fromJson(msg, Map.class);
        System.out.println(map.get("refData"));
        
    }

    static void print_Map(List<String> list) throws Exception {
        System.out.println("source:  " + list.get(0));
        System.out.println("replace: " + list.get(0).replaceAll("\\\\\\\\n", ""));
        Map<String, String> map = Jacksons.parse(list.get(0).replaceAll("\\\\\\\\n", ""), Map.class);
        Map<String, String> map2 = Jacksons.parse(list.get(0), Map.class);
        System.out.println(map);
        System.out.println(map2);

        map = Jacksons.parse(list.get(2).replaceAll("\\\\n", ""), Map.class);
        map2 = Jacksons.parse(list.get(2), Map.class);
        System.out.println(map);
        System.out.println(map2);

        System.out.println("ceshi\n测试\nhhhhhhh");
        Map hello = new HashMap<>();
        hello.put("more", "ceshi\n测试\nhhhhhhh");
        hello.put("test",
                "{\\\"keywords\\\":[\\\"841297684467\\\"],\\\"roomId\\\":950611902122214400,\\\"sentence\\\":\\\"微信聊吧加我的号841297684467\\\\n\\\"}");
        Map inner = ImmutableMap.of("innerKey1", "InNER", "INNER_K2", "hello\nworld\n 测试");
        Map outer = new HashMap<>();
        outer.put("outer", "outerval");
        outer.put("outer-inner", inner);

        hello.put("inner", outer);
        // System.out.println(JsonUtils.toJson(hello));
        System.out.println(Jacksons.string(hello));
    }
    
    public static class Outer{
        private String biz;
        private Request request;
        public String getBiz() {
            return biz;
        }
        public void setBiz(String biz) {
            this.biz = biz;
        }
        public Request getRequest() {
            return request;
        }
        public void setRequest(Request request) {
            this.request = request;
        }
        
    }
    public static class Request{
        private String sourcetype;
        private Inner inner;
        public String getSourcetype() {
            return sourcetype;
        }
        public void setSourcetype(String sourcetype) {
            this.sourcetype = sourcetype;
        }
        public Inner getInner() {
            return inner;
        }
        public void setInner(Inner inner) {
            this.inner = inner;
        }
        
    }
    public static class Inner{
        private String info;
        private String version;
        public String getInfo() {
            return info;
        }
        public void setInfo(String info) {
            this.info = info;
        }
        public String getVersion() {
            return version;
        }
        public void setVersion(String version) {
            this.version = version;
        }
    }
}
